package com.SmartCare.controller.client;

import com.SmartCare.common.Result;
import com.SmartCare.service.ImageService;
import com.SmartCare.service.goodMemories.GoodMemoriesService;
import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.Operation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/goodMemory")
@Api(tags = "用户端美好瞬间")
@Slf4j
public class GoodMemoriesController {
    @Resource
    ImageService imageService;

    @Value("${files.upload.path}/goodMemoriesImg/")
    private String fileUploadPath;

    @Value("${careHome.files.upload.save.url}goodMemory/")
    private String fileSavePath;
    @Resource
    private GoodMemoriesService goodMemService;

    //添加图片
    @PostMapping("/upload")
    @Operation(summary = "上传图片文件", description = "将返回该图片的下载地址")
    public Result addImage(@RequestParam List<MultipartFile> file) {
        return imageService.upload(file, fileUploadPath, "goodMemory");
    }

    //下载图片
    @GetMapping("/{fileUuid}")
    @Operation(summary = "上传图片文件", description = "直接访问该路径可下载图片")
    public void download(@PathVariable String fileUuid, HttpServletResponse response) throws IOException {
        imageService.download(fileUuid, fileUploadPath, response);
    }

    @GetMapping("/download")
    @Operation(summary = "下载图片文件", description = "通过该接口可下载多个图片文件")
    public void downloads(@RequestParam("fileUuids") List<String> fileUuids, HttpServletResponse response) throws IOException {
        imageService.downloads(fileUuids, fileUploadPath, response);
    }

    @GetMapping("/add")
    public Result add(@RequestParam Long userId,
                      @RequestParam List<String> photoUrls,
                      @RequestParam(required = false) String content) {
        return goodMemService.saveAll(userId, photoUrls, content);
    }
    /* @GetMapping("/view")
    public Result view(){
        return goodMemService.view();
    } */

    @GetMapping("/delete")
    @Transactional
    public Result delete(@RequestParam List<Long> ids) {
        List<String> fileUuids = goodMemService.remove(ids);
        List<String> list;
        if (fileUuids.isEmpty()) {
            return Result.success("删除成功");
        } else {
            list = imageService.delete(fileUuids, fileUploadPath, fileSavePath);
            if (list.isEmpty()) {
                return Result.success("删除成功");
            }
        }
        return Result.error("未删除文件名称", list);
    }

    @GetMapping("/query")
    public Result getPage(@RequestParam String careHomeName,
                          @RequestParam Integer page,
                          @RequestParam Integer pageSize){
        return goodMemService.getPage(careHomeName, page, pageSize);
    }
}
